<template>
  <div>
    <el-form size="mini">
      <el-form-item label="选择优惠方式：">
        <el-select v-model="product.promotionType" placeholder="请选择" class="active-select">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-show="product.promotionType===1">
        <div>
          开始时间：
          <el-date-picker
            v-model="product.promotionStartTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm ss"
            :picker-options="pickerOptions1"
            placeholder="选择开始时间"
          ></el-date-picker>
        </div>
        <div class="littleMargin">
          结束时间：
          <el-date-picker
            v-model="product.promotionStartTime"
            value-format="yyyy-MM-dd HH:mm ss"
            type="datetime"
            :picker-options="pickerOptions1"
            placeholder="选择结束时间"
          ></el-date-picker>
        </div>
        <div class="littleMargin">
          促销价格：
          <el-input style="width: 220px" v-model="product.promotionPrice" placeholder="输入促销价格"></el-input>
        </div>
      </el-form-item>
      <el-form-item v-show="product.promotionType===2">
        <div v-for="(item, index) in product.memberPriceList" :class="{littleMargin:index!==0}">
          {{item.memberLevelName}}：
          <el-input v-model="item.memberPrice" style="width: 200px"></el-input>
        </div>
      </el-form-item>
      <el-form-item v-show="product.promotionType===3">
        <el-button type="text" @click="handleAddProductLadder()">添加</el-button>
        <el-table :data="product.productLadderList" border>
          <el-table-column label="数量" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.count"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="折扣" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.discount"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="handleRemoveProductLadder(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item v-show="product.promotionType===4">
        <el-button type="text" @click="handleAddFullReduction()">添加</el-button>
        <el-table :data="product.productFullReductionList" border>
          <el-table-column label="满">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fullPrice"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="立减">
            <template slot-scope="scope">
              <el-input v-model="scope.row.reducePrice"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="handleRemoveFullReduction(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <van-goods-action>
        <van-button block type="primary" @click="onSubmit">保存</van-button>
      </van-goods-action>
    </el-form>
  </div>
</template>

<script>
import {
  creatProduct,
  updateProduct,
  productinfo,
  brandlist,
  categorylist
} from "@/api/product";
export default {
  name: "projectInfo",
  props: {
    value: {
      default: null
    }
  },
  watch: {
    value: {
      handler(newValue, oldValue) {
        this.product = newValue;
      },
      immediate: true
    }
  },
  data() {
    return {
      options: [
        {
          value: 0,
          label: "没有促销使用原价"
        },
        {
          value: 1,
          label: "使用促销价"
        },
        {
          value: 2,
          label: "使用会员价"
        },
        {
          value: 3,
          label: "使用阶梯价格"
        },
        {
          value: 4,
          label: "使用满减价格"
        },
        {
          value: 5,
          label: "使用限时购"
        }
      ],
      product: {},
      selectActive: "",
      //日期选择器配置
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() < Date.now();
        }
      }
    };
  },
  methods: {
    handleEditCreated() {
      let ids = this.value.serviceIds.split(",");
      console.log("handleEditCreated", ids);
      for (let i = 0; i < ids.length; i++) {
        this.selectServiceList.push(Number(ids[i]));
      }
    },
    handleRemoveProductLadder(index, row) {
      let productLadderList = this.value.productLadderList;
      if (productLadderList.length === 1) {
        productLadderList.pop();
        productLadderList.push({
          count: 0,
          discount: 0,
          price: 0
        });
      } else {
        productLadderList.splice(index, 1);
      }
    },
    handleAddProductLadder() {
      let productLadderList = this.value.productLadderList;
      if (productLadderList.length < 3) {
        productLadderList.push({
          count: 0,
          discount: 0,
          price: 0
        });
      } else {
        this.$message({
          message: "最多只能添加三条",
          type: "warning"
        });
      }
    },
    handleRemoveFullReduction(index, row) {
      let fullReductionList = this.value.productFullReductionList;
      if (fullReductionList.length === 1) {
        fullReductionList.pop();
        fullReductionList.push({
          fullPrice: 0,
          reducePrice: 0
        });
      } else {
        fullReductionList.splice(index, 1);
      }
    },
    handleAddFullReduction() {
      let fullReductionList = this.value.productFullReductionList;
      if (fullReductionList.length < 3) {
        fullReductionList.push({
          fullPrice: 0,
          reducePrice: 0
        });
      } else {
        this.$message({
          message: "最多只能添加三条",
          type: "warning"
        });
      }
    },

    onSubmit() {
      this.$emit("commit", this.product);
    }
  }
};
</script>
<style scoped>
.active-select {
  width: 100%;
}
</style>